This code analyses splitting statistics for CTC-clusters.
The analysis takes a list of trees sampled from its posterior distribution as input and samples mutations placements for each of the trees.
inputFolder <- "/Users/jgawron/Documents/projects/CTC_backup/input_folder"
simulationInputFolder <- "/Users/jgawron/Documents/projects/CTC_backup/simulations/simulations2"
treeName <- "LM2"
nTreeSamplingEvents <- 1000
nMutationSamplingEvents <- 1000
source("/Users/jgawron/Documents/projects/CTC-SCITE/CTC-SCITE/experiments/workflow/resources/functions.R")
## ── Attaching core tidyverse packages ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
input <- load_data(inputFolder, treeName)
## Rows: 40000 Columns: 5
## ── Column specification ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (1): Tree
## dbl (4): LogScore, SequencingErrorRate, DropoutRate, LogTau
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 883 Columns: 72
## ── Column specification ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (3): X1, X3, X4
## dbl (69): X2, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, X16, X17, X1...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 34 Columns: 5
## ── Column specification ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (2): Cluster, Description
## dbl (3): CellCount, TCs, WBCs
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
postSampling <- input$postSampling
nClusters <- input$nClusters
ClusterID <- input$clusterID
nCells <- input$nCells
nMutations <- input$nMutations
nClusters <- input$nClusters
alleleCount <- input$alleleCount
mutatedReadCounts <- input$mutatedReadCounts
totalReadCounts <- input$totalReadCounts
sampleDescription <- input$sample_description
Each row corresponds to a cell. Column description: - Cluster: An number indicating which sample the cell belongs to. - ClusterName: The name of the sample in the nodeDescription.tsv file - WBC: a binary vector indicating whether the cell is a white blood cell (1) or not (0). - color: Indicates the color of the cluster in the tree, as described in the nodeDescription.tsv file.
print(sampleDescription)
## Cluster ClusterName WBC color single_cell
## 1 0 LM2_A81 0 gray93 TRUE
## 2 1 LM2_A83 0 gray93 TRUE
## 3 2 LM2_A85 0 gray93 TRUE
## 4 3 LM2_A86 0 lightcoral FALSE
## 5 3 LM2_A86 0 lightcoral FALSE
## 6 4 LM2_A89 0 gray93 TRUE
## 7 5 LM2_A90 0 gray93 TRUE
## 8 6 LM2_A93 0 sandybrown FALSE
## 9 6 LM2_A93 0 sandybrown FALSE
## 10 7 LM2_A94 0 skyblue3 FALSE
## 11 7 LM2_A94 0 skyblue3 FALSE
## 12 8 LM2_CTC_G19 0 thistle TRUE
## 13 9 LM2_CTC_G21 0 thistle TRUE
## 14 10 LM2_CTC_G32 0 lemonchiffon TRUE
## 15 11 LM2_CTC_G33 0 lemonchiffon TRUE
## 16 12 LM2_CTC_G34 0 lemonchiffon TRUE
## 17 13 LM2_E1 0 gray93 TRUE
## 18 14 LM2_E2 0 gray93 TRUE
## 19 15 LM2_E3 0 gray93 TRUE
## 20 16 LM2_E5 0 violetred3 FALSE
## 21 16 LM2_E5 0 violetred3 FALSE
## 22 17 LM2_E6 0 gray93 TRUE
## 23 18 LM2_E7 0 lightslateblue FALSE
## 24 18 LM2_E7 0 lightslateblue FALSE
## 25 19 LM2_G1 0 paleturquoise3 FALSE
## 26 19 LM2_G1 0 paleturquoise3 FALSE
## 27 20 LM2_G10 0 khaki3 FALSE
## 28 20 LM2_G10 0 khaki3 FALSE
## 29 21 LM2_G11 0 gray93 TRUE
## 30 22 LM2_G12 0 gray93 TRUE
## 31 23 LM2_G13 0 gray93 TRUE
## 32 24 LM2_G14 0 darkseagreen4 FALSE
## 33 24 LM2_G14 0 darkseagreen4 FALSE
## 34 25 LM2_G15 0 gold FALSE
## 35 25 LM2_G15 0 gold FALSE
## 36 25 LM2_G15 0 gold FALSE
## 37 25 LM2_G15 0 gold FALSE
## 38 25 LM2_G15 0 gold FALSE
## 39 26 LM2_G2 0 plum TRUE
## 40 27 LM2_G3 1 plum FALSE
## 41 27 LM2_G3 0 plum FALSE
## 42 27 LM2_G3 0 plum FALSE
## 43 28 LM2_G4 0 yellowgreen TRUE
## 44 29 LM2_G5 0 yellowgreen TRUE
## 45 30 LM2_G6 0 yellowgreen TRUE
## 46 31 LM2_G7 0 yellowgreen TRUE
## 47 32 LM2_G8 1 yellowgreen FALSE
## 48 32 LM2_G8 0 yellowgreen FALSE
## 49 33 LM2_G9 0 navajowhite2 FALSE
## 50 33 LM2_G9 0 navajowhite2 FALSE
Get null distributions of relevant statistics, stratified by sample:
cutoffsSplittingProbs <- data.frame(clusterSize = vector(), Cutoff = vector())
cutoffsBranchingProbabilities <- data.frame(clusterSize = vector(), Cutoff = vector())
for (clusterSize in 2:5){
try(
{treeNameSimulated <- paste(treeName, clusterSize, sep = '_')
inputSimulated <- load_data(simulationInputFolder, treeNameSimulated)
postSamplingSimulated <- inputSimulated$postSampling
nClustersSimulated <- inputSimulated$nClusters
ClusterIDSimulated <- inputSimulated$clusterID
nCellsSimulated <- inputSimulated$nCells
nMutationsSimulated <- inputSimulated$nMutations
nClustersSimulated <- inputSimulated$nClusters
alleleCountSimulated <- inputSimulated$alleleCount
mutatedReadCountsSimulated <- inputSimulated$mutatedReadCounts
totalReadCountsSimulated <- inputSimulated$totalReadCounts
sampleDescriptionSimulated <- inputSimulated$sample_description
distance <- computeClusterSplits(sampleDescriptionSimulated, postSamplingSimulated, treeNameSimulated, nCellsSimulated,
nMutationsSimulated, nClustersSimulated,
alleleCountSimulated,
mutatedReadCountsSimulated, totalReadCountsSimulated,
nMutationSamplingEvents = nMutationSamplingEvents, nTreeSamplingEvents = nTreeSamplingEvents,
cellPairSelection = c("orchid", "orchid1", "orchid2",
"orchid3", "orchid4", "darkorchid",
"darkorchid1","darkorchid2", "darkorchid3",
"darkorchid4", "purple", "purple1",
"purple2", "purple3", "purple4"))
plot(ggplot(distance$splittingProbs, aes(x = "Values", y = Splitting_probability, fill = 'Splitting_probability')) +
geom_boxplot())
cutoffsSplittingProbs <- rbind(cutoffsSplittingProbs, data.frame(clusterSize = clusterSize, Cutoff = mean(distance$splittingProbs$Splitting_probability) + 2 * sd(distance$splittingProbs$Splitting_probability) ))
##Note that the way the aggregatedBranchingProbabilities are computed all pairs of cells from the same cluster are
## taken into account. This has the effect that clusters with more cells would be counted more often and contribute more
## to the shape of the final distribution. This is no problem right now as we only aggregate counts from clusters
## of the same size, it is however the potential source of a future bug!!
plot(ggplot(data.frame(x = distance$aggregatedBranchingProbabilities), aes(x = x)) +
geom_histogram(binwidth = 0.01))
print(data.frame(clusterSize = clusterSize, Cutoff = quantile(distance$aggregatedBranchingProbabilities, probs = 0.95, names = FALSE)[1] ))
cutoffsBranchingProbabilities <- rbind(cutoffsBranchingProbabilities, data.frame(clusterSize = clusterSize, Cutoff = quantile(distance$aggregatedBranchingProbabilities, probs = 0.95, names = FALSE)[1] ))
})
}
## Rows: 65960 Columns: 5
## ── Column specification ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (1): Tree
## dbl (4): LogScore, SequencingErrorRate, DropoutRate, LogTau
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 883 Columns: 80
## ── Column specification ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (3): X1, X3, X4
## dbl (77): X2, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, X16, X17, X1...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 38 Columns: 5
## ── Column specification ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (2): Cluster, Description
## dbl (3): CellCount, TCs, WBCs
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## [1] "Computing genomic distances of leaves: 51 50"
## [1] "Computing the posterior distribution"
## [1] "Computing genomic distances of leaves: 53 52"
## [1] "Computing the posterior distribution"
## [1] "Computing genomic distances of leaves: 55 54"
## [1] "Computing the posterior distribution"
## [1] "Computing genomic distances of leaves: 57 56"
## [1] "Computing the posterior distribution"
## clusterSize Cutoff
## 1 2 6.306453e-06
## Rows: 25118 Columns: 5
## ── Column specification ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (1): Tree
## dbl (4): LogScore, SequencingErrorRate, DropoutRate, LogTau
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 883 Columns: 78
## ── Column specification ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (3): X1, X3, X4
## dbl (75): X2, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, X16, X17, X1...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 37 Columns: 5
## ── Column specification ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (2): Cluster, Description
## dbl (3): CellCount, TCs, WBCs
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## [1] "Computing genomic distances of leaves: 51 50"
## [1] "Computing the posterior distribution"
## [1] "Computing genomic distances of leaves: 54 53"
## [1] "Computing the posterior distribution"
## [1] "Computing genomic distances of leaves: 57 56"
## [1] "Computing the posterior distribution"
## clusterSize Cutoff
## 1 3 3.75624e-06
## Rows: 24534 Columns: 5
## ── Column specification ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (1): Tree
## dbl (4): LogScore, SequencingErrorRate, DropoutRate, LogTau
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 883 Columns: 76
## ── Column specification ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (3): X1, X3, X4
## dbl (73): X2, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, X16, X17, X1...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 36 Columns: 5
## ── Column specification ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (2): Cluster, Description
## dbl (3): CellCount, TCs, WBCs
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## [1] "Computing genomic distances of leaves: 51 50"
## [1] "Computing the posterior distribution"
## [1] "Computing genomic distances of leaves: 55 54"
## [1] "Computing the posterior distribution"
## clusterSize Cutoff
## 1 4 2.807941e-06
## Warning: One or more parsing issues, call `problems()` on your data frame for details, e.g.:
## dat <- vroom(...)
## problems(dat)
## Rows: 54437 Columns: 5
## ── Column specification ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (1): Tree
## dbl (4): LogScore, SequencingErrorRate, DropoutRate, LogTau
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 883 Columns: 76
## ── Column specification ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (3): X1, X3, X4
## dbl (73): X2, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, X16, X17, X1...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 36 Columns: 5
## ── Column specification ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (2): Cluster, Description
## dbl (3): CellCount, TCs, WBCs
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## [1] "Computing genomic distances of leaves: 51 50"
## [1] "Computing the posterior distribution"
## [1] "Computing genomic distances of leaves: 56 55"
## [1] "Computing the posterior distribution"
## clusterSize Cutoff
## 1 5 3.219289e-06
Get the relevant statistics for each of the clusters of a dataset and output numbers of oligoclonal clusters:
nTumorClusters <- 0
nOligoclonalClusters1 <- 0
nOligoclonalClusters2 <- 0
splittingSummary1 <- data.frame(Color = vector(), Oligoclonal = vector(), ClusterSize = vector())
splittingSummary2 <- data.frame(Color = vector(), Oligoclonal = vector(), ClusterSize = vector())
for(clusterSize in 2:5){
try({
clusterColor <- sampleDescription %>%
filter(WBC ==0 & color != 'gray93') %>%
group_by(color) %>%
filter(n() == clusterSize) %>%
pull(color) %>%
unique()
for(color in clusterColor){
distance <- computeClusterSplits(sampleDescription, postSampling, treeName, nCells,
nMutations, nClusters,
alleleCount,
mutatedReadCounts, totalReadCounts,
nMutationSamplingEvents = nMutationSamplingEvents, nTreeSamplingEvents = nTreeSamplingEvents,
cellPairSelection = c(color))
splittingProbs <- mean(distance$splittingProbs$Splitting_probability)
branchingProbs <- mean(distance$aggregatedBranchingProbabilities)
nTumorClusters <- nTumorClusters + 1
oligoclonal <- FALSE
print(clusterSize)
print(cutoffsSplittingProbs[(cutoffsSplittingProbs$clusterSize == clusterSize), 2])
if(splittingProbs > (cutoffsSplittingProbs[(cutoffsSplittingProbs$clusterSize == clusterSize), 2])){
nOligoclonalClusters1 <- nOligoclonalClusters1 + 1
oligoclonal <- TRUE
}
splittingSummary1 <- rbind(splittingSummary1, data.frame(Color = color, Oligoclonal = oligoclonal, ClusterSize = clusterSize))
oligoclonal <- FALSE
if(branchingProbs > cutoffsBranchingProbabilities[(cutoffsBranchingProbabilities$clusterSize == clusterSize), 2]){
nOligoclonalClusters2 <- nOligoclonalClusters2 + 1
oligoclonal <- TRUE
}
splittingSummary2 <- rbind(splittingSummary2, data.frame(Color = color, Oligoclonal = oligoclonal, ClusterSize = clusterSize))
}
})
}
## [1] "Computing genomic distances of leaves: 4 3"
## [1] "Computing the posterior distribution"
## [1] 2
## [1] 0.08619833
## [1] "Computing genomic distances of leaves: 8 7"
## [1] "Computing the posterior distribution"
## [1] 2
## [1] 0.08619833
## [1] "Computing genomic distances of leaves: 10 9"
## [1] "Computing the posterior distribution"
## [1] 2
## [1] 0.08619833
## [1] "Computing genomic distances of leaves: 12 11"
## [1] "Computing the posterior distribution"
## [1] 2
## [1] 0.08619833
## [1] "Computing genomic distances of leaves: 20 19"
## [1] "Computing the posterior distribution"
## [1] 2
## [1] 0.08619833
## [1] "Computing genomic distances of leaves: 23 22"
## [1] "Computing the posterior distribution"
## [1] 2
## [1] 0.08619833
## [1] "Computing genomic distances of leaves: 25 24"
## [1] "Computing the posterior distribution"
## [1] 2
## [1] 0.08619833
## [1] "Computing genomic distances of leaves: 27 26"
## [1] "Computing the posterior distribution"
## [1] 2
## [1] 0.08619833
## [1] "Computing genomic distances of leaves: 32 31"
## [1] "Computing the posterior distribution"
## [1] 2
## [1] 0.08619833
## [1] "Computing genomic distances of leaves: 49 48"
## [1] "Computing the posterior distribution"
## [1] 2
## [1] 0.08619833
## [1] "Computing genomic distances of leaves: 14 13"
## [1] "Computing the posterior distribution"
## [1] 3
## [1] 0.007393762
## [1] "Computing genomic distances of leaves: 40 38"
## [1] "Computing the posterior distribution"
## [1] 3
## [1] 0.007393762
## [1] "Computing genomic distances of leaves: 34 33"
## [1] "Computing the posterior distribution"
## [1] 5
## [1] 0.01719647
## [1] "Computing genomic distances of leaves: 43 42"
## [1] "Computing the posterior distribution"
## [1] 5
## [1] 0.01719647
numberOfCancerClusters <- sampleDescription %>%
filter(WBC ==0 & color != 'gray93') %>%
group_by(color) %>%
filter(n() > 1) %>%
pull(color) %>%
unique() %>% length()
print(sprintf('%d out of %d clusters were found to be oligoclonal in %s, using method 1', nOligoclonalClusters1, numberOfCancerClusters, treeName))
## [1] "8 out of 14 clusters were found to be oligoclonal in LM2, using method 1"
print(sprintf('%d out of %d clusters were found to be oligoclonal in %s, using method 2', nOligoclonalClusters2, numberOfCancerClusters, treeName))
## [1] "11 out of 14 clusters were found to be oligoclonal in LM2, using method 2"
print(splittingSummary1)
## Color Oligoclonal ClusterSize
## 1 lightcoral FALSE 2
## 2 sandybrown FALSE 2
## 3 skyblue3 FALSE 2
## 4 thistle TRUE 2
## 5 violetred3 TRUE 2
## 6 lightslateblue FALSE 2
## 7 paleturquoise3 TRUE 2
## 8 khaki3 FALSE 2
## 9 darkseagreen4 TRUE 2
## 10 navajowhite2 FALSE 2
## 11 lemonchiffon TRUE 3
## 12 plum TRUE 3
## 13 gold TRUE 5
## 14 yellowgreen TRUE 5
print(splittingSummary2)
## Color Oligoclonal ClusterSize
## 1 lightcoral FALSE 2
## 2 sandybrown FALSE 2
## 3 skyblue3 FALSE 2
## 4 thistle TRUE 2
## 5 violetred3 TRUE 2
## 6 lightslateblue TRUE 2
## 7 paleturquoise3 TRUE 2
## 8 khaki3 TRUE 2
## 9 darkseagreen4 TRUE 2
## 10 navajowhite2 TRUE 2
## 11 lemonchiffon TRUE 3
## 12 plum TRUE 3
## 13 gold TRUE 5
## 14 yellowgreen TRUE 5